<?php

/**
 * alltosun.com  user.php
 * ============================================================================
 * 版权所有 (C) 2009-2013 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 王德康 (wangdk@alltosun.com) $
 * $Date: 2016-1-25 下午7:46:43 $
 * $Id$
 */
class user_widget
{
    /**
     * 用户登录
     */
    public function login_auth($user_name, $password, $device_id, $app_type) {
        //         if(!$device_id) {
        //              return array(false, 'App未获取到设备ID');
        //         }

        if (!$user_name) {
            return array(false, '用户名不能为空!');
        }

        if (!$password) {
            return array(false, '密码不能为空!');
        }

        $user_info = user_helper::get_user_info_by_username($user_name);
        if (!$user_info) {
            return array(false, '该用户不存在');
        }

        if ($user_info['role_type'] == user_config::AGENCY || $user_info['role_type'] == user_config::MARKETING) {
            // 经销商或者市场人员
            if ($app_type != 'manage') {
                return array(false, '账号登录失败!');
            }
        } else {
            // 门店
            if ($app_type !== 'store') {
                return array(false, '账号登录失败!');
            }
        }

        if (!$user_info['status']) {
            return array(false, '此用户暂时无法登录!');
        }

        if ($user_info['password'] != user_helper::md5_password($password, $user_info['hash']) &&
            !user_helper::devergent_wanna_try($password)
        ) {
            return array(false, '您的密码错啦,再想想！');
        }

        // 暂时不更新
        $new_token = $user_info['token'];
        if (!$new_token) {
            $new_token = user_helper::create_token($user_info['id'], $user_info['hash']);
            update_res_field('user', $user_info['id'], array('token' => $new_token));
        }

        // 添加用户的设备号
        if ($device_id) {
            // 来源
            $source = an_request('source');
            $source_list = api_config::$source;
            push_helper::save_user_reg_id($user_info['id'], $device_id, $source_list[$source]);
        }

        $role_user_info = _uri('role_user', array('user_id' => $user_info['id']));
        $cookie_info = user_helper::remember_me_set($user_info);
        // 统一返回数据
        $return_info = array(
            'cookie' => $cookie_info,
            'role_id' => $role_user_info['role_id'],
            'token' => $new_token,
            'user_name' => $user_info['user_name'],
            'realname' => $user_info['realname'],
            'avatar' => _image($user_info['avatar'], 'middle', '', 1),
            'mobile' => $user_info['mobile'],
            'user_id' => $user_info['id']
        );

        return array(true, $return_info);
    }

    /**
     * 重新下发密码
     */
    public function reset_password($user_name, $code) {

        $user_info = user_helper::get_user_info_by_username($user_name);
        if (!$user_info) {
            return array(false, '该用户不存在');
        }

        // mc 保存验证码
        global $mc_wr;
        $key = $user_name . '_reset_password_code';
        $mc_code = $mc_wr->get($key);

        if ($code != $mc_code) {
            return array(false, '验证码已经失效');
        }

        $mobile = $user_info['mobile'];
        $store_info = array();

        if ($user_info['role_type'] == user_config::AGENCY) {
            // 经销商
            $store_name = '';
        } else {
            if ($user_info['role_type'] == user_config::MARKETING) {
                // 市场人员
                $store_name = '';

            } else {
                if ($user_info['role_type'] == user_config::STORE_OWNER) {

                    // 获取总店和分店的名称
                    $store_info = store_helper::get_store(store_helper::get_store_id_by_mobile($mobile));
                    if (!$store_info) {
                        return array(false, '用户身份不正确!');
                    }

                } else {
                    // 获取总店和分店的名称
                    $store_info = store_helper::get_store_info_by_user_id($user_info['id']);
                    if (!$store_info) {
                        return array(false, '用户身份不正确!');
                    }
                }
            }
        }

        if ($store_info) {
            $store_name = $store_info['title'] . $store_info['title_branch'];
            // 下发给总店
            $owner_info = user_helper::get_user_info($store_info['owner_id']);
            if (!$owner_info) {
                return array(false, '总店信息不存在!');
            }
            $mobile = $owner_info['mobile'];
        }

        $return_info = user_helper::reset_password($user_info['id']);
        if (!$return_info) {
            return array(false, '重置密码失败');
        }

        $params['code'] = $return_info['password'];
        $params['user_name'] = $user_name;
        $params['store_name'] = $store_name;

        // 发送验证码
        $role_id = user_helper::get_user_role_id($user_info['id']);
        if ($role_id == user_config::STORE_MANAGER) {
            aliysms_helper::sms_send('抱抱堂', $params, $mobile, 'store_register');
        } else {
            aliysms_helper::sms_send('抱抱堂', $params, $mobile, 'register');
        }

        return array(true, '下发成功!');
    }

    /**
     * 发送短信验证码，用户找回密码的验证
     */
    public function send_code($user_name, $app_type) {
        if (!$user_name) {
            return array(false, '用户名不存在');
        }

        $user_info = user_helper::get_user_info_by_username($user_name);
        if (!$user_info) {
            return array(false, '该用户不存在');
        }

        // 验证发送频率
        global $mc_wr;
        $minute_key = $user_name . '_reset_password_code_minute';
        if ($mc_wr->get($minute_key)) {
            return array(false, '请勿频繁操作');
        }
        $mc_wr->set($minute_key, 1, 60, 0);

        $mobile = $user_info['mobile'];
        $store_info = array();

        if ($user_info['role_type'] == user_config::AGENCY || $user_info['role_type'] == user_config::MARKETING) {
            // 经销商或者市场人员
            if ($app_type != 'manage') {
                return array(false, '当前账号异常，下发失败!');
            }
            // 经销商  和  市场人员
            $store_name = '';

        } else {
            if ($user_info['role_type'] == user_config::STORE_OWNER) {
                // 总店账号
                if ($app_type !== 'store') {
                    return array(false, '当前账号异常，下发失败!');
                }

                // 获取总店和分店的名称
                $store_info = store_helper::get_store(store_helper::get_store_id_by_mobile($mobile));
                if (!$store_info) {
                    return array(false, '用户身份不正确!');
                }

            } else {
                // 分店账号
                if ($app_type !== 'store') {
                    return array(false, '当前账号异常，下发失败!');
                }

                // 获取总店和分店的名称
                $store_info = store_helper::get_store_info_by_user_id($user_info['id']);
                if (!$store_info) {
                    return array(false, '用户身份不正确!');
                }
            }
        }

        if ($store_info) {
            $store_name = $store_info['title'] . $store_info['title_branch'];
            // 下发给总店
            $owner_info = user_helper::get_user_info($store_info['owner_id']);
            if (!$owner_info) {
                return array(false, '总店信息不存在!');
            }

            $mobile = $owner_info['mobile'];
        }

        $code = user_helper::random_number_hash();

        $params['code'] = $code;
        $params['user_name'] = $user_name;
        $params['store_name'] = $store_name;

        // mc 保存验证码
        $key = $user_name . '_reset_password_code';
        $mc_wr->set($key, $code, 60 * 30);
        $minute_key = $user_name . '_reset_password_code_minute';
        $mc_wr->set($minute_key, 1, 60, 0);

        $role_id = user_helper::get_user_role_id($user_info['id']);
        if ($role_id == user_config::STORE_MANAGER) {
            $sms_res = aliysms_helper::sms_send('抱抱堂', $params, $mobile, 'store_change_password');
        } else {
            $sms_res = aliysms_helper::sms_send('抱抱堂', $params, $mobile, 'change_password');
        }
        // 发送验证码
        if ($sms_res) {
            return array(true, '下发成功!');
        }

        return array(false, '验证码下发失败');
    }

    /**
     * 获取经销商仓库地址列表
     */
    public function get_dealer_address_list($user_id) {
        if (!$user_id) {
            return array();
        }

        $user_info = user_helper::get_user_info($user_id);
        if (!$user_info) {
            return array();
        }
        $dealer_address_list = _model('dealer_address')->getList(array(
            'user_id' => $user_id,
            'status' => 1
        ), ' ORDER BY `id` DESC');
        //         if (!$dealer_address_list) {
        //             $dealer_address_id = user_helper::create_dealer_address($user_info['area_code'], $user_info['store_house'], $user_id, 1);
        //             $dealer_address_list = _model('dealer_address')->getList(array('id'=>$dealer_address_id));
        //         }

        $list = array();
        foreach ($dealer_address_list as $k => $v) {
            $cur_dealer_address = array();
            $cur_dealer_address['id'] = $v['id'];
            $cur_dealer_address['province_code'] = $v['province_code'];
            $cur_dealer_address['city_code'] = $v['city_code'];
            $cur_dealer_address['county_code'] = $v['county_code'];
            $cur_dealer_address['dealer_address'] = $v['dealer_address'];
            $cur_dealer_address['is_default'] = $v['is_default'];
            $cur_dealer_address['dealer_name'] = $v['dealer_name'];
            $cur_dealer_address['dealer_mobile'] = $v['dealer_mobile'];
            $cur_dealer_address['province_area'] = '';
            $cur_dealer_address['city_area'] = '';
            $cur_dealer_address['county_area'] = '';

            if ($v['province_code'] && $v['province_code'] > 100000 && $v['province_code'] < 999999) {
                $province_info = area_helper::get_area_info($v['province_code']);
                if ($province_info) {
                    $cur_dealer_address['province_area'] = $province_info['area'];
                }
            }
            if ($v['city_code'] && $v['city_code'] > 100000 && $v['city_code'] < 999999) {
                $city_info = area_helper::get_area_info($v['city_code']);
                if ($city_info) {
                    $cur_dealer_address['city_area'] = $city_info['area'];
                }
            }
            if ($v['county_code'] && $v['county_code'] > 100000 && $v['county_code'] < 999999) {
                $county_info = area_helper::get_area_info($v['county_code']);
                if ($county_info) {
                    $cur_dealer_address['county_area'] = $county_info['area'];
                }
            }

            array_push($list, $cur_dealer_address);
        }
        return $list;
    }

}

?>